import { UserLoginApi, getIndexListApi, getCategoryLeftApi, getCategoryRightApi, postCategoryListApi, postBrandListApi } from "@/api/index";
const actions = {
    //提交mutations中的方法，达到修改状态的目的，通常异步请求放在这里
    async actChangeIsLogin(context, params) {
        //进行ajax请求
        let result = await UserLoginApi(params.user_name, params.user_password)
        // console.log(result);
        if (result.status == 200) {
            localStorage.setItem("token", JSON.stringify(result.token))
            context.commit("changeIsLogin", result.token)
        }
    },
    // 首页列表
    async actChangeIndexListDatas(context, params) {
        context.commit("changeIsLoading", true)
        let result = await getIndexListApi(params)
        console.log(result);
        if (result.status == "success") {
            context.commit("changeIndexListDatas", result.data)
        }
    },
    // 分类左侧
    async actChangeCategoryLeftDatas(context) {
        let result = await getCategoryLeftApi()
        console.log(result);
        context.commit("changeCategoryLeftDatas", result.data)
    },
    // 分类右侧
    async actChangeCategoryRightDatas(context, cat_id) {
        context.commit("changeIsLoading", true)
        let result = await getCategoryRightApi(cat_id)
        console.log(result);
        if (result.status == "success") {
            context.commit("changeIsLoading", false)
        }
        context.commit("changeCategoryRightDatas", result.data)
    },
    //分类列表
    async actChangeCategoryListDatas(context, params) {
        context.commit("changeIsLoading", true)
        let result = await postCategoryListApi(params)
        console.log(result);
        if (result.data.length != 0) {
            context.commit("changeCategoryListDatas", result.data)
            context.commit("changeIsLoading", false)
        } else {
            context.commit("changeIsLoading", false)

        }

    },
    // 品牌列表
    async actChangeBrandListDatas(context, params) {
        let result = await postBrandListApi(params)
        console.log(result);
        context.commit("changeBrandListDatas", result.data)
    }
}

export default actions